package com.leyou.page.service.impl;

import com.leyou.item.pojo.*;
import com.leyou.page.cilent.BrandClient;
import com.leyou.page.cilent.GoodsClient;
import com.leyou.page.cilent.SpecClient;
import com.leyou.page.service.PageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PageServiceImpl implements PageService {

    @Autowired
    private GoodsClient goodsClient;

    @Autowired
    private SpecClient specClient;

    @Autowired
    private BrandClient brandClient;


    @Override
    public Map<String, Object> loadData(Long spuId) {
        Map<String, Object> map = new HashMap<>();
        //根据spuId查询spu
        Spu spu = this.goodsClient.querySpuById(spuId);
        //根据spuid查询brand的name
        String brandName = this.brandClient.queryBrandByIds(spuId);
        map.put("spu", spu);
        map.put("brandName", brandName);
        //根据spu的id查询spuDetail
        SpuDetail spuDetail = this.goodsClient.querySpuDetailById(spuId);
        map.put("spuDetail", spuDetail);
        //根据spuId查询sku
        List<Sku> skus = this.goodsClient.querySkuBySpuId(spuId);
        map.put("skus", skus);
        Map<Long, Object> specmap = new HashMap<>();
        List<SpecParam> specParams = this.specClient.querySpecParam(null, spu.getCid3(), null, false);
        specParams.forEach(specParam -> specmap.put(specParam.getId(), specParam.getName()));
        map.put("specs", specmap);
        List<SpecParam> specParams2 = this.specClient.querySpecParam(null, spu.getCid3(), null, true);
        map.put("specs2", specParams2);
        List<SpecGroup> specGroups = this.specClient.querySpecGroups(spu.getCid3());
        map.put("specGroups", specGroups);
        return map;
    }
}
